package com.googlecode.jascii.ap;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JComponent;

import org.apache.commons.io.FileUtils;

import com.googlecode.jascii.TextReportReader;
import com.googlecode.jascii.printing.FontUtils;

public class TextFileComponent  extends JComponent  implements Printable{

	private float fontSize;
	private Font font ;
	private String encode = null;
	private TextReportReader textReader ;
	private Dimension mPreferredSize;
	public TextFileComponent(File file, PageFormat pageFormat)
    throws IOException {
		fontSize = 12;
		Font fireflyFontDynamic = FontUtils.getFont("fireflysung.ttf");
		font = fireflyFontDynamic.deriveFont(fontSize  );

		// Open the file.
		textReader =  new TextReportReader(file,encode);
		
		 mPreferredSize = new Dimension((int)pageFormat.getImageableWidth(),
			        (int)pageFormat.getImageableHeight());
		
	
}
	public TextFileComponent(File file, PageFormat pageFormat,String encode)
    throws IOException {
		fontSize = 12;
		Font fireflyFontDynamic = FontUtils.getFont("fireflysung.ttf");
		font = fireflyFontDynamic.deriveFont(fontSize  );

		// Open the file.
		textReader =  new TextReportReader(file,encode);
		
		 mPreferredSize = new Dimension((int)pageFormat.getImageableWidth(),
			        (int)pageFormat.getImageableHeight());
		
	
}
	public void paginate(PageFormat pageFormat) {

	    mPreferredSize = new Dimension((int)pageFormat.getImageableWidth(),
	        (int)pageFormat.getImageableHeight());
	    repaint();
	  }
	
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
			throws PrinterException {
		 if (pageIndex >= textReader.getPages()) return NO_SUCH_PAGE;
		    Graphics2D g2 =  (Graphics2D)graphics.create();
		   g2.clearRect(0, 0,(int) pageFormat.getImageableX(),(int) pageFormat.getImageableY());
		    g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
		    printText(  g2);
		    //paint(g2);
		    return PAGE_EXISTS;
  	}
	 public void paintComponent(Graphics g) {
		    Graphics2D g2 = (Graphics2D)g;
		    java.awt.geom.Rectangle2D r = new java.awt.geom.Rectangle2D.Float(0, 0,
		        mPreferredSize.width, mPreferredSize.height);
		    g2.setPaint(Color.LIGHT_GRAY);
		    g2.fill(r);
		    printText( g2);
		   
		  }
	 private void printText( Graphics2D g2)
	 {
		 // Get the current page.
		    String pageStr = textReader.getCurrentPage();
		    // Draw all the lines for this page.
		    g2.setFont(font);
		    g2.setPaint(Color.black);
		    float x = 0;
		    float y = fontSize;
		    String[] page = pageStr.split("\n"); 
		    for (int i = 0; i < page.length; i++) {
		      String line = (String)page[i];
		      for(int itemp=0;itemp<line.length();itemp++)
		      {
		    	  char ich = line.charAt(itemp);
		    	  String istr = String.valueOf(ich);
		    	  
		    	  if(istr.getBytes().length==1)
		    	  {
		    		  g2.drawString(String.valueOf(line.charAt(itemp)), (int)x, (int)y);
		    	      x+=fontSize/2;
		    	  }
		    	  else
		    	  {
		    		  g2.drawString(String.valueOf(line.charAt(itemp)), (int)x, (int)y);
		    		  x+=fontSize;
		    	  }
		    		  
		      }
		      y += fontSize;
		      x=0;
		    }
	 }
	 public Dimension getPreferredSize() { return mPreferredSize; }
	  
	  public int getCurrentPage() { return textReader.getCurrentPageIndex(); }
	  public int getNumPages() { return textReader.getPages(); }
	  
	  public void nextPage() {
		textReader.nextPage();
	    repaint();
	  }
	  
	  public void previousPage() {
		  textReader.previousPage();
	    repaint();
	  }
}
